import hashlib
import pyDes
import rsa
import base64
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5  # 用于加密/ 解密

from Crypto.Cipher import DES3
import base64


def hashlib_md5(parm):
    m2 = hashlib.md5()
    m2.update(parm.encode('utf-8'))
    return m2.hexdigest()


#
def sha256hex(data):
    sha256 = hashlib.sha256()
    sha256.update(data.encode())
    res = sha256.hexdigest()
    print("sha256加密结果:", res)
    return res


BS = DES3.block_size


def pad(s):
    return s + (BS - len(s) % BS) * chr(BS - len(s) % BS)


def unpad(s):
    return s[0:-ord(s[-1])]


class prpcrypt():
    def __init__(self):
        self.token = "c9a88f4206416de82c839a077c71f67f "
        self.mode = DES3.MODE_ECB

    def get_key(self):
        return hashlib_md5(self.token + self.token)[:24]

    def move_value(self):
        return hashlib_md5(self.token + self.token)[:8]

    def encrypt(self, text):
        text = pad(text)
        key = self.get_key()
        print(key)
        cryptor = DES3.new(key, self.mode, IV=self.move_value())
        x = len(text) % 8
        if x != 0:
            text = text + '\0' * (8 - x)
        # print(text)
        self.ciphertext = cryptor.encrypt(text.encode())
        # return base64.standard_b64encode(self.ciphertext).decode("utf-8")

        k = pyDes.des(key, pyDes.CBC, self.move_value(), pad=None)
        d = k.encrypt(text.encode())
        print(str(base64.b64encode(d), encoding="utf-8"))


    def decrypt(self, text):
        cryptor = DES3.new(self.get_key(), self.mode)
        de_text = base64.standard_b64decode(text)
        plain_text = cryptor.decrypt(de_text)
        st = str(plain_text.decode("utf-8")).rstrip('\0')
        out = unpad(st)
        return out


if __name__ == '__main__':
    d = prpcrypt().encrypt("12345")
    print(d)
